package com.nordstrom;

import java.io.IOException;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.URIException;
import org.archive.crawler.datamodel.CrawlURI;
import org.archive.crawler.extractor.Extractor;
import org.archive.crawler.extractor.Link;
import org.archive.io.ReplayCharSequence;
import org.archive.util.HttpRecorder;

public class NordStromExtractor extends Extractor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public NordStromExtractor(String name) {

		this(name, "Sohu News Extractor");

	}

	public NordStromExtractor(String name, String description) {
		super(name, description);
	}

	public static final String PATTERN_A_HREF = "<a\\s.*?href=\"([^\"]+)\"[^>]*>";

	protected void extract(CrawlURI curi) {
		ReplayCharSequence cs = null;
		try {
			HttpRecorder hr = curi.getHttpRecorder();
			if (hr == null) {
				throw new IOException("Why is recorder null here?");
			}
			cs = hr.getReplayCharSequence();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (cs == null) {
			return;
		}
		String content = cs.toString(); 
		try {
			Pattern pattern = Pattern.compile(PATTERN_A_HREF,
					Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(content);
			while (matcher.find()) {
				String newUrl = matcher.group(1);
				try
				{
					addLinkFromString(curi, newUrl, "", Link.EMBED_HOP);
				}catch(Exception ex)
				{
					ex.printStackTrace();
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addLinkFromString(CrawlURI curi, String uri, CharSequence context, char hopType) {  
		
		try {
			curi.createAndAddLinkRelativeToBase(uri, context, hopType);
			
		} catch (URIException e) 
		{
		}  }}
